/*
 * init_cc.c
 *
 *  Created on: 2012/08/01
 *      Author: owner
 */

#include <stdio.h>
#include "sqlite3.h"
#include "typedef.h"
#include "db_def.h"
#include "core.h"
#include "Id.h"

/*TBL_DECLARATION*/
static INT createDeclaration(sqlite3 *db) {

	INT rc = 0;
	PSZ zErrMsg = NULL;
	PSZ sql_create = NULL;

	sql_create =
			sqlite3_mprintf(
					"CREATE TABLE %q (TblUnit INTEGER, TblIndex INTEGER,"
							"%q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER,PRIMARY KEY(TblUnit,TblIndex));",
					TBL_DECLARATION, _T_IDX(3), _T_IDX(4), _T_IDX(5),
					_T_IDX(6 ), _T_IDX(7 ), _T_IDX(8 ), _T_IDX(9 ),
					_T_IDX(10 ));
	rc = sqlite3_exec(db, sql_create, 0, 0, &zErrMsg);
	if (rc != SQLITE_OK) {
		return rc;
	} else {
		if (sql_create != NULL ) {
			sqlite3_free(sql_create);
		}
	}
	return rc;
}

/*TBL_DECLARATOR*/
static INT createDeclarator(sqlite3 *db) {

	INT rc = 0;
	PSZ zErrMsg = NULL;
	PSZ sql_create = NULL;

	sql_create =
			sqlite3_mprintf(
					"CREATE TABLE %q (TblUnit INTEGER, TblIndex INTEGER,"
							"%q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER,"
							"%q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER,PRIMARY KEY(TblUnit,TblIndex));",
					TBL_DECLARATOR, _T_IDX(3), _T_IDX(4), _T_IDX(5), _T_IDX(6),
					_T_IDX(7 ), _T_IDX(8), _T_IDX(9), _T_IDX(10), _T_IDX(11),
					_T_IDX(12), _T_IDX(13));
	rc = sqlite3_exec(db, sql_create, 0, 0, &zErrMsg);
	if (rc != SQLITE_OK) {
		return rc;
	} else {
		if (sql_create != NULL ) {
			sqlite3_free(sql_create);
		}
	}
	return rc;
}

/*TBL_TYPESPECIFIER*/
static INT createTypeSpecifier(sqlite3 *db) {

	INT rc = 0;
	PSZ zErrMsg = NULL;
	PSZ sql_create = NULL;

	sql_create =
			sqlite3_mprintf(
					"CREATE TABLE %q (TblUnit INTEGER, TblIndex INTEGER,"
							"%q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER,"
							"%q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER,"
							"%q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER,PRIMARY KEY(TblUnit,TblIndex));",
					TBL_TYPESPECIFIER, _T_IDX(3), _T_IDX(4), _T_IDX(5),
					_T_IDX(6), _T_IDX(7), _T_IDX(8), _T_IDX(9), _T_IDX(10),
					_T_IDX(11), _T_IDX(12), _T_IDX(13), _T_IDX(14), _T_IDX(15),
					_T_IDX(16), _T_IDX(17), _T_IDX(18), _T_IDX(19), _T_IDX(20),
					_T_IDX(21), _T_IDX(22), _T_IDX(23), _T_IDX(24),
					_T_IDX(25));
	rc = sqlite3_exec(db, sql_create, 0, 0, &zErrMsg);
	if (rc != SQLITE_OK) {
		return rc;
	} else {
		if (sql_create != NULL ) {
			sqlite3_free(sql_create);
		}
	}
	return rc;
}

/*TBL_EXPRESSION*/
static INT createExpression(sqlite3 *db) {

	INT rc = 0;
	PSZ zErrMsg = NULL;
	PSZ sql_create = NULL;

	sql_create =
			sqlite3_mprintf(
					"CREATE TABLE %q (TblUnit INTEGER, TblIndex INTEGER,"
							"%q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER,"
							"%q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER,PRIMARY KEY(TblUnit,TblIndex));",
					TBL_EXPRESSION, _T_IDX(3), _T_IDX(4 ), _T_IDX(5), _T_IDX(6),
					_T_IDX(7), _T_IDX(8), _T_IDX(9), _T_IDX(10), _T_IDX(11),
					_T_IDX(12), _T_IDX(13), _T_IDX(14 ), _T_IDX(15),
					_T_IDX(16));
	rc = sqlite3_exec(db, sql_create, 0, 0, &zErrMsg);
	if (rc != SQLITE_OK) {
		return rc;
	} else {
		if (sql_create != NULL ) {
			sqlite3_free(sql_create);
		}
	}
	return rc;
}

/*TBL_EXPRESSIONLIST*/
static INT createExpressionList(sqlite3 *db) {

	INT rc = 0;
	PSZ zErrMsg = NULL;
	PSZ sql_create = NULL;

	sql_create =
			sqlite3_mprintf(
					"CREATE TABLE %q (TblUnit INTEGER, TblIndex INTEGER,"
							"%q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER,PRIMARY KEY(TblUnit,TblIndex));",
					TBL_EXPRESSIONLIST, _T_IDX(3), _T_IDX(4), _T_IDX(5),
					_T_IDX(6), _T_IDX(7), _T_IDX(8), _T_IDX(9), _T_IDX(10),
					_T_IDX(11));
	rc = sqlite3_exec(db, sql_create, 0, 0, &zErrMsg);
	if (rc != SQLITE_OK) {
		return rc;
	} else {
		if (sql_create != NULL ) {
			sqlite3_free(sql_create);
		}
	}
	return rc;
}

/*TBL_INITIALIZERDESIGNATION*/
static INT createInitializerDesignation(sqlite3 *db) {

	INT rc = 0;
	PSZ zErrMsg = NULL;
	PSZ sql_create = NULL;

	sql_create =
			sqlite3_mprintf(
					"CREATE TABLE %q (TblUnit INTEGER, TblIndex INTEGER,"
							"%q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER,PRIMARY KEY(TblUnit,TblIndex));",
					TBL_INITIALIZERDESIGNATION, _T_IDX(3), _T_IDX(4), _T_IDX(5),
					_T_IDX(6), _T_IDX(7), _T_IDX(8), _T_IDX(9));
	rc = sqlite3_exec(db, sql_create, 0, 0, &zErrMsg);
	if (rc != SQLITE_OK) {
		return rc;
	} else {
		if (sql_create != NULL ) {
			sqlite3_free(sql_create);
		}
	}
	return rc;
}

/*TBL_STATEMENT*/
static INT createStatement(sqlite3 *db) {

	INT rc = 0;
	PSZ zErrMsg = NULL;
	PSZ sql_create = NULL;

	sql_create =
			sqlite3_mprintf(
					"CREATE TABLE %q (TblUnit INTEGER, TblIndex INTEGER,"
							"%q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER,"
							"%q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER,PRIMARY KEY(TblUnit,TblIndex));",
					TBL_STATEMENT, _T_IDX(3), _T_IDX(4 ), _T_IDX(5 ),
					_T_IDX(6 ), _T_IDX(7 ), _T_IDX(8 ), _T_IDX(9), _T_IDX(10),
					_T_IDX(11), _T_IDX(12), _T_IDX(13), _T_IDX(14),
					_T_IDX(15));
	rc = sqlite3_exec(db, sql_create, 0, 0, &zErrMsg);
	if (rc != SQLITE_OK) {
		return rc;
	} else {
		if (sql_create != NULL ) {
			sqlite3_free(sql_create);
		}
	}
	return rc;
}

/*TBL_SYMBOLINFO*/
static INT createSymbolInfo(sqlite3 *db) {

	INT rc = 0;
	PSZ zErrMsg = NULL;
	PSZ sql_create = NULL;

	sql_create =
			sqlite3_mprintf(
					"CREATE TABLE %q (TblUnit INTEGER, TblIndex INTEGER,"
							"%q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q TEXT,"
							"%q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER,PRIMARY KEY(TblUnit,TblIndex));",
					TBL_SYMBOLINFO, _T_IDX(3), _T_IDX(4 ), _T_IDX(5), _T_IDX(6),
					_T_IDX(7), _T_IDX(8), _T_IDX(9), _T_IDX(10), _T_IDX(11 ),
					_T_IDX(12), _T_IDX(13), _T_IDX(14), _T_IDX(15));
	rc = sqlite3_exec(db, sql_create, 0, 0, &zErrMsg);
	if (rc != SQLITE_OK) {
		return rc;
	} else {
		if (sql_create != NULL ) {
			sqlite3_free(sql_create);
		}
	}
	return rc;
}

/*TBL_LINKAGE*/
static INT createLinkage(sqlite3 *db) {

	INT rc = 0;
	PSZ zErrMsg = NULL;
	PSZ sql_create = NULL;

	sql_create =
			sqlite3_mprintf(
					"CREATE TABLE %q (TblUnit INTEGER, TblIndex INTEGER,"
							"%q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER,PRIMARY KEY(TblUnit,TblIndex));",
					TBL_LINKAGE, _T_IDX(3), _T_IDX(4), _T_IDX(5), _T_IDX(6),
					_T_IDX(7), _T_IDX(8 ));
	rc = sqlite3_exec(db, sql_create, 0, 0, &zErrMsg);
	if (rc != SQLITE_OK) {
		return rc;
	} else {
		if (sql_create != NULL ) {
			sqlite3_free(sql_create);
		}
	}
	return rc;
}

/*TBL_TYPEINFO*/
static INT createTypeInfo(sqlite3 *db) {

	INT rc = 0;
	PSZ zErrMsg = NULL;
	PSZ sql_create = NULL;

	sql_create =
			sqlite3_mprintf(
					"CREATE TABLE %q (TblUnit INTEGER, TblIndex INTEGER,"
							"%q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER,"
							"%q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER,PRIMARY KEY(TblUnit,TblIndex));",
					TBL_TYPEINFO, _T_IDX(3), _T_IDX(4), _T_IDX(5), _T_IDX(6),
					_T_IDX(7), _T_IDX(8), _T_IDX(9), _T_IDX(10), _T_IDX(11),
					_T_IDX(12), _T_IDX(13 ), _T_IDX(14 ), _T_IDX(15 ));
	rc = sqlite3_exec(db, sql_create, 0, 0, &zErrMsg);
	if (rc != SQLITE_OK) {
		return rc;
	} else {
		if (sql_create != NULL ) {
			sqlite3_free(sql_create);
		}
	}
	return rc;
}

/*TBL_VALUEINFO*/
static INT createValueInfo(sqlite3 *db) {

	INT rc = 0;
	PSZ zErrMsg = NULL;
	PSZ sql_create = NULL;

	sql_create =
			sqlite3_mprintf(
					"CREATE TABLE %q (TblUnit INTEGER, TblIndex INTEGER, %q INTEGER, %q INTEGER, %q DOUBLE,PRIMARY KEY(TblUnit,TblIndex));",
					TBL_VALUEINFO, _T_IDX(3 ), _T_IDX(4), _T_IDX(5));
	rc = sqlite3_exec(db, sql_create, 0, 0, &zErrMsg);
	if (rc != SQLITE_OK) {
		return rc;
	} else {
		if (sql_create != NULL ) {
			sqlite3_free(sql_create);
		}
	}
	return rc;
}

/*TBL_UNITECHAIN*/
static INT createUniteChain(sqlite3 *db) {

	INT rc = 0;
	PSZ zErrMsg = NULL;
	PSZ sql_create = NULL;

	sql_create =
			sqlite3_mprintf(
					"CREATE TABLE %q (TblUnit INTEGER, TblIndex INTEGER,"
							"%q INTEGER, %q INTEGER, %q INTEGER, %q INTEGER,PRIMARY KEY(TblUnit,TblIndex));",
					TBL_UNITECHAIN, _T_IDX(3), _T_IDX(4 ), _T_IDX(5),
					_T_IDX(6));
	rc = sqlite3_exec(db, sql_create, 0, 0, &zErrMsg);
	if (rc != SQLITE_OK) {
		return rc;
	} else {
		if (sql_create != NULL ) {
			sqlite3_free(sql_create);
		}
	}
	return rc;
}

/*initialize cc db*/
INT DLL_EXPORT initCcDB(sqlite3 * coreData) {
	INT rc = 0;

	rc = createDeclaration(coreData);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = createDeclarator(coreData);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = createTypeSpecifier(coreData);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = createExpression(coreData);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = createExpressionList(coreData);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = createInitializerDesignation(coreData);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = createStatement(coreData);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = createSymbolInfo(coreData);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = createLinkage(coreData);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = createTypeInfo(coreData);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = createValueInfo(coreData);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	rc = createUniteChain(coreData);
	if (rc != COEXIT_SUCCESS) {
		return rc;
	}

	return COEXIT_SUCCESS;
}
